﻿
var BindOldCard = require('./BindOldCard');
var Profileform = require('./Profileform');
var React = require('react');


var ProfileBox = React.createClass({
    loadCommentsFromServer: function () {
        $.ajax({
            url: this.props.url,
            dataType: 'json',
            cache: false,
            success: function (data) {
                data.cellno = this.props.initialData.cellno;
                data.dycardno = this.props.initialData.dycardno;
                this.setState({ data: data });
            }.bind(this),
            error: function (xhr, status, err) {
                alert(err.toString());
            }.bind(this)
        });
    },
    handleCommentSubmit: function (comment) {
        // alert(JSON.stringify(comment));
        $("#submitbnt").attr("disabled", "disabled");
        $.ajax({
            url: this.props.submitUrl,
            dataType: 'json',
            type: 'POST',
            data: comment,
            success: function (data) {
                if (data.Issuccess == false) {
                    $().toastmessage('showErrorToast', data.errormsg);
                }
                else {
                    $().toastmessage('showSuccessToast', "个人资料更新成功！");
                }
                $("#submitbnt").removeAttr("disabled");
                this.setState({ data: data });
            }.bind(this),
            error: function (xhr, status, err) {
                alert(err.toString());
                console.error(this.props.url, status, err.toString());
            }.bind(this)
        });
    },
    getInitialState: function () {
        return { data: [] };
    },
    componentDidMount: function () {
        this.loadCommentsFromServer();
    },
    render: function () {
        return (
            <Profileform  data={this.state.data} onCommentSubmit={this.handleCommentSubmit} />
        );
    }
});

module.exports = ProfileBox;